<?php
/**
 * 新闻列表页
 */
 
define('PAGESIZE', 20);

class News extends CI_Controller {

    public function index()
    {
        $cid = $this->input->get('cid',TRUE);
        $page = $this->input->get('page',TRUE);
        
        $this->load->model('category');
        $this->load->model('thread');
        
        $is_exist = $this->category->getCategory(array('cid'=>$cid),0,1);
        
        if(!$is_exist) {
            show_404();
        }
        
        //分页设置
        $count = $this->thread->countAll($cid);
        $max_page = (int)floor($count / PAGESIZE + 1);
        
        if(!isset($page) || !is_numeric($page)) {
            $page = 1;
        }
        
        if($page < 1) {
            $page = 1;
        }
        
        if($page > $max_page) {
            $page = $max_page;
        }
        
        //分页输出
        $data['page']['count']   = $max_page;
        $data['page']['prev']    = ($page > 1) ? $page - 1 : 1;
        $data['page']['next']    = ($page < $max_page) ? $page + 1 : $max_page;
        $data['page']['current'] = $page;
        $offset = ($page-1)*PAGESIZE;
        
        $data['title'] = $this->category->getCategory(array('cid'=>$cid),0,1);        
        $data['list'] = $this->thread->getThreadList(array('cid'=>$cid),$offset,PAGESIZE);
        $data['cid'] = $cid;
        $site = parent::getSiteValue();
        
        $data['cid_7'] = $this->thread->getThreadList(array('cid'=>7),0,5);
        $data['cid_8'] = $this->thread->getHotList();
        $data['cid_9'] = $this->thread->getThreadList(array('cid'=>9),0,5);
        $data['cid_10'] = $this->thread->getThreadList(array('cid'=>10),0,5); 
        
        $this->load->view('common/header',$data);
        $this->load->view('news',$data);
        $this->load->view('common/footer',$site);
    }
}